package jwdt.wizards;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jwdt.common.Constant;
import jwdt.popup.actions.DevToolsAction;
import jwdt.utils.ClassUtils;
import jwdt.utils.ConnectionUtils;
import jwdt.utils.HibernateTypeResolver;
import jwdt.utils.JWDTSettingUtils;
import jwdt.utils.TypeResolver;
import jwdt.utils.VelocityUtils;
import jwdt.utils.XmlConfigUtils;
import jwdt.vo.DevToolsVo;
import jwdt.vo.InitProDataSourceVo;
import jwdt.vo.ProjectFileTypeVo;
import jwdt.vo.TableVo;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;

public class DevToolsWizard extends Wizard implements INewWizard {
	private DevToolsWizardPage_1 page1;
	private ISelection selection;

	/**
	 * Constructor for InitProFrameWorkWizard.
	 */
	public DevToolsWizard(ISelection selection) {
		super();
		setNeedsProgressMonitor(true);
		this.selection = selection;
	}
	
	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		page1 = new DevToolsWizardPage_1(selection);
		addPage(page1);
	}

	/**
	 * This method is called when 'Finish' button is pressed in
	 * the wizard. 
	 */
	public boolean performFinish() {		
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					
					DevToolsVo devToolsVo = page1.getDevToolsVo();
					InitProDataSourceVo initProDataSourceVo = devToolsVo.getInitProDataSourceVo();
										
					//---------------
					Connection con = null;
					try {
									
						con = ConnectionUtils.getConnection(initProDataSourceVo.getDriverClass(), 
															initProDataSourceVo.getDataBaseUrl(), 
															initProDataSourceVo.getUserName(), 
															initProDataSourceVo.getPassWord(),
															DevToolsAction.javaProject);
						
						List<TableVo> tableVos = ConnectionUtils.getTableVos(con, devToolsVo.getTableFileMap().keySet());
						
						IPackageFragmentRoot ipfr = DevToolsAction.javaProject
						.findPackageFragmentRoot(new Path("/"
								+ DevToolsAction.projectProperties.getProjectName()
								+ "/"
								+ devToolsVo.getJavaSourcesPath()));
						
						for(TableVo tableVo : tableVos){
							if(tableVo.getPrimaryKeys() == null){
								MessageDialog.openError(getShell(), "错误", "表["+tableVo.getTableName()+"]未检测到主键，请检查...");
								return;
							}	
							if(tableVo.getPrimaryKeys().size() > 1){
								MessageDialog.openError(getShell(), "错误", "表["+tableVo.getTableName()+"]检测到联合主键，推荐使用业务无关的单一主键...");
								return;
							}
							
							ProjectFileTypeVo projectFileTypeVo =  devToolsVo.getTableFileMap().get(tableVo.getTableName());
							/*
							 * ORM
							 */
							if(projectFileTypeVo.isOrm()){	
								if(Constant.ORM_HIBERNATE.equals(DevToolsAction.projectProperties.getFrameWorks().get(Constant.ORM))){
									//PO
									loadFiles4PO(ipfr, devToolsVo, tableVo);
									//HBM
									loadFiles4HBM(ipfr, devToolsVo, tableVo);
									//config
									hibernateConfigUpdate(devToolsVo, tableVo);
								}else {
									//PO
									loadFiles4PO(ipfr, devToolsVo, tableVo);
									//SQLMAP
									loadFiles4SQLMAP(ipfr, devToolsVo, tableVo);
									//config
									ibatisConfigUpdate(devToolsVo, tableVo);
								}
								
							}
							/*
							 * DAO
							 */
							if(projectFileTypeVo.isDao()){								
								//IDAO
								loadFiles4DAO(ipfr,devToolsVo,tableVo);
								//DAOImpl
								if(Constant.ORM_HIBERNATE.equals(DevToolsAction.projectProperties.getFrameWorks().get(Constant.ORM))){
									loadFiles4DAOImplhbm(ipfr, devToolsVo, tableVo);
								}else{
									loadFiles4DAOImpl(ipfr, devToolsVo, tableVo);
								}								
								//config
								springConfigUpdateDao(devToolsVo, tableVo);
							}							
							/*
							 * SERVICE
							 */
							if(projectFileTypeVo.isService()){
								//ISERVICE
								loadFiles4SERVICE(ipfr,devToolsVo,tableVo);
								//SERVICEImpl
								loadFiles4SERVICEImpl(ipfr,devToolsVo,tableVo);
								//config
								springConfigUpdateService(devToolsVo, tableVo);
							}							
							/*
							 * WEB
							 */
							if(projectFileTypeVo.isWeb()){
								if(Constant.MVC_STRUTS.equals(DevToolsAction.projectProperties.getFrameWorks().get(Constant.MVC))){
									
								}else if(Constant.MVC_STRUTS2.equals(DevToolsAction.projectProperties.getFrameWorks().get(Constant.MVC))){
									//Action2
									loadFiles4Action2(ipfr, devToolsVo, tableVo);
								}else{
									//默认springMVC
									//Controller
									loadFiles4Controller(ipfr,devToolsVo,tableVo);
								}								
							}
							/*
							 * PAGE
							 */
							if(projectFileTypeVo.isPage()){
								//PAGE
								loadFiles4Page(devToolsVo, tableVo);
							}							
						}
						
						
					} catch (Exception e) {	
						e.printStackTrace();
						MessageDialog.openError(getShell(), "错误", "请检查数据库配置或项目中的驱动程序是否存在...");
					} finally {
						if (null != con) {
							try {
								con.close();
							} catch (Exception ex) {}				
						}
					}
					//-------------
					
					//setp4 : 生成.jwdt.devtools
					JWDTSettingUtils.saveSettings(DevToolsAction.projectProperties.getProjectPath(), page1.getDevToolsVo());
					
					// 刷新项目
					DevToolsAction.javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
				} catch (Exception e) {		
					e.printStackTrace();
					MessageDialog.openError(getShell(), "错误", e.getMessage());
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}		
		
		return true;
	}
	//loadFiles
	private void loadFiles4PO(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".orm.po";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}
		
		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className = ClassUtils.formatClassName(tableVo.getTableName());
		
		templateFile = "templates/devtools/PO.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFiles4SQLMAP(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".orm.sqlmap";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}
		
		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className = ClassUtils.formatClassName(tableVo.getTableName());
		
		if(Constant.DB_TYPE_MYSQL.equals(devToolsVo.getInitProDataSourceVo().getDataBaseType())){
			templateFile = "templates/devtools/SQLMAP_MYSQL.vm";
		}
		if(Constant.DB_TYPE_ORACLE.equals(devToolsVo.getInitProDataSourceVo().getDataBaseType())){
			templateFile = "templates/devtools/SQLMAP_ORACLE.vm";
		}	
		outFilePath = srcPath + "/" + className + ".xml";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFiles4HBM(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".orm.hbm";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}
		
		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className = ClassUtils.formatClassName(tableVo.getTableName());
		
		templateFile = "templates/devtools/HBM.vm";
		outFilePath = srcPath + "/" + className + ".hbm.xml";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new HibernateTypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void hibernateConfigUpdate(DevToolsVo devToolsVo,TableVo tableVo)throws Exception{
		String configFilePath = DevToolsAction.projectProperties.getProjectPath()+"/"+DevToolsAction.projectProperties.getJavaSourcesPath()[0]+"/hibernate.cfg.xml";
		File file=new File(configFilePath);
		String resource = devToolsVo.getPackageText()+".orm.hbm."+ClassUtils.formatClassName(tableVo.getTableName());
		resource = resource.replaceAll("\\.", "/")+".hbm.xml";
		XmlConfigUtils.hibernateConfigUpdate(file, resource);
	}
	private void ibatisConfigUpdate(DevToolsVo devToolsVo,TableVo tableVo)throws Exception{
		String configFilePath = DevToolsAction.projectProperties.getProjectPath()+"/"+DevToolsAction.projectProperties.getWebappPath()+"/WEB-INF/config/sqlMapConfig.xml";
		File file=new File(configFilePath);
		String resource = devToolsVo.getPackageText()+".orm.sqlmap."+ClassUtils.formatClassName(tableVo.getTableName());
		resource = resource.replaceAll("\\.", "/")+".xml";
		XmlConfigUtils.ibatisConfigUpdate(file, resource);
	}
	private void loadFiles4DAO(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".dao";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}

		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className="I" + ClassUtils.formatClassName(tableVo.getTableName()) + "Dao";
		
		templateFile = "templates/devtools/IDAO.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFiles4DAOImpl(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".dao.impl";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}

		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className=ClassUtils.formatClassName(tableVo.getTableName()) + "DaoImpl";
		
		templateFile = "templates/devtools/DAOImpl.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		if(DevToolsAction.projectProperties.getInitProDataSourceVoMap().size() > 1){
			parameters.put("dataSourceKey", devToolsVo.getInitProDataSourceVo().getDataSourceName());
		}else{
			parameters.put("dataSourceKey", null);
		}		
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFiles4DAOImplhbm(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".dao.impl";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}

		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className=ClassUtils.formatClassName(tableVo.getTableName()) + "DaoImpl";
		
		templateFile = "templates/devtools/DAOImplHBM.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		if(DevToolsAction.projectProperties.getInitProDataSourceVoMap().size() > 1){
			parameters.put("dataSourceKey", devToolsVo.getInitProDataSourceVo().getDataSourceName());
		}else{
			parameters.put("dataSourceKey", null);
		}		
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void springConfigUpdateDao(DevToolsVo devToolsVo,TableVo tableVo)throws Exception{
		String configFilePath = DevToolsAction.projectProperties.getProjectPath()+"/"+DevToolsAction.projectProperties.getWebappPath()+"/WEB-INF/config/model-config.xml";
		File file=new File(configFilePath);
		
		String daoId = ClassUtils.formatPropertyName(tableVo.getTableName())+"Dao";
		
		String daoClass = devToolsVo.getPackageText()+".dao.impl."+ClassUtils.formatClassName(tableVo.getTableName())+"DaoImpl";
		
		XmlConfigUtils.springConfigUpdateDao(file,  daoId, daoClass);
	}
	private void loadFiles4SERVICE(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".service";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}

		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className="I" + ClassUtils.formatClassName(tableVo.getTableName()) + "Service";
		
		templateFile = "templates/devtools/ISERVICE.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFiles4SERVICEImpl(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".service.impl";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}

		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className=ClassUtils.formatClassName(tableVo.getTableName()) + "ServiceImpl";
		
		templateFile = "templates/devtools/SERVICEImpl.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void springConfigUpdateService(DevToolsVo devToolsVo,TableVo tableVo)throws Exception{
		String configFilePath = DevToolsAction.projectProperties.getProjectPath()+"/"+DevToolsAction.projectProperties.getWebappPath()+"/WEB-INF/config/model-config.xml";
		File file=new File(configFilePath);
		
		String daoId = ClassUtils.formatPropertyName(tableVo.getTableName())+"Dao";
		String serviceId = ClassUtils.formatPropertyName(tableVo.getTableName())+"Service";		
		String serviceClass = devToolsVo.getPackageText()+".service.impl."+ClassUtils.formatClassName(tableVo.getTableName())+"ServiceImpl";
		
		XmlConfigUtils.springConfigUpdateService(file, serviceId, serviceClass, daoId);
	}
	private void loadFiles4Controller(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = devToolsVo.getPackageText()+".web";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}

		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className=ClassUtils.formatClassName(tableVo.getTableName()) + "Controller";
		
		templateFile = "templates/devtools/CONTROLLER.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFiles4Action2(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		//action
		loadFiles4Action2Action(ipfr, devToolsVo, tableVo);
		//abstratct
		loadFiles4Action2Abstratct(ipfr, devToolsVo, tableVo);
		
	}
	private void loadFiles4Action2Action(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		//action
		String packageStr = devToolsVo.getPackageText()+".web";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}
				
		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className=ClassUtils.formatClassName(tableVo.getTableName()) + "Action";
		
		templateFile = "templates/devtools/ACTION2.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);		
	}
	private void loadFiles4Action2Abstratct(IPackageFragmentRoot ipfr,DevToolsVo devToolsVo,TableVo tableVo) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		//abstract action
		String packageStr = devToolsVo.getPackageText()+".web.abstracts";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}
				
		String srcPath = DevToolsAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		String className="Abstract" + ClassUtils.formatClassName(tableVo.getTableName()) + "Action";
		
		templateFile = "templates/devtools/ABSTRACTACTION2.vm";		
		outFilePath = srcPath + "/" + className + ".java";
		
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("packageStr", devToolsVo.getPackageText());
		parameters.put("className", className);
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);		
	}
	private void loadFiles4Page(DevToolsVo devToolsVo,TableVo tableVo) throws Exception {
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		
		String outPath = DevToolsAction.projectProperties.getProjectPath()+"/"+DevToolsAction.projectProperties.getWebappPath()+"/WEB-INF/pages/jsp/"+ClassUtils.formatClassName(tableVo.getTableName()).toLowerCase();
		File file = new File(outPath);
		if(!file.exists()){
			file.mkdirs();
		}
		
		templateFile = "templates/devtools/PAGE.vm";
		outFilePath = outPath+"/mainpage.jsp";
		parameters.put("projectName", DevToolsAction.projectProperties.getProjectName());
		parameters.put("tableVo", tableVo);
		parameters.put("ClassUtils", new ClassUtils());
		parameters.put("TypeResolver", new TypeResolver());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	/**
	 * We will accept the selection in the workbench to see if
	 * we can initialize from it.
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
	
	@Override
	public boolean canFinish() {
		if(page1.getDevToolsVo().getTableFileMap().size() < 1){
			return false;
		}
		return super.canFinish();
	}
}